﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace GameBasicsLibrary
{
    public class CModel
    {
        public Vector3 Position { set; get; }
        public Vector3 Rotation { set; get; }
        public Vector3 Scale { set; get; }
        public Model model { private set; get; }
        //Porque un Array de matrices??? por cada hueso?
        private Matrix[] modelTransforms;
        private GraphicsDevice graphicsDevice;

        public CModel(Model model, Vector3 Position, Vector3 Rotation, Vector3 Scale, GraphicsDevice graphicsDevice)
        {
            this.model = model;
            this.Position = Position;
            this.Rotation = Rotation;
            this.Scale = Scale;
            // sin esto no funciona el model transforms, hay que rellenar la matriz
            modelTransforms = new Matrix[model.Bones.Count];

            model.CopyAbsoluteBoneTransformsTo(modelTransforms);

            this.graphicsDevice = graphicsDevice;
        }
        public Vector3 ToRadians(Vector3 baseRot){

            return new Vector3(MathHelper.ToRadians(baseRot.X), MathHelper.ToRadians(baseRot.Y), MathHelper.ToRadians(baseRot.Z));
        }
        public void Draw(Matrix cameraView, Matrix cameraProjection)
        {
            //Calcular el transform del modelo
            Vector3 rotRad = this.ToRadians(this.Rotation);
            Matrix baseWorld = Matrix.CreateScale(this.Scale) * Matrix.CreateFromYawPitchRoll(rotRad.Y, rotRad.X, rotRad.Z);

            //baseWorld*=Matrix.CreateFromQuaternion(
            //if (rotRad.X != 0)
            //{
            //    baseWorld *= Matrix.CreateRotationX(rotRad.X);
            //}
            //if (rotRad.Y != 0)
            //{
            //    baseWorld *= Matrix.CreateRotationY(rotRad.Y);
            //}
            //if (rotRad.Z != 0)
            //{
            //    baseWorld *= Matrix.CreateRotationZ(rotRad.Z);
            //}

            //Añadir Translacion
            baseWorld *= Matrix.CreateTranslation(this.Position);
            foreach (ModelMesh mesh in model.Meshes)
            {

                //creamos el espacio local multiplicando el
                Matrix localWorld;
                localWorld = this.modelTransforms[mesh.ParentBone.Index] * baseWorld;

                foreach (ModelMeshPart part in mesh.MeshParts)
                {
                    BasicEffect e = (BasicEffect)part.Effect;

                    e.World = localWorld;
                    e.View = cameraView;
                    e.Projection = cameraProjection;
                    e.EnableDefaultLighting();
                }

                mesh.Draw();
            }
        }
    }
}
